package com.pz.cloud.framework.leagueMem.model;

import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import com.pz.cloud.framework.util.BeanUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.model.BaseImportModel;
import org.jeecg.common.util.BeanConvertUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 团员导入
 * @author mzz
 */
@Component
@Slf4j
public class LeagueMemImportModel extends BaseImportModel<LeagueMem> {

    @Autowired
    private ILeagueMemService leagueMemService;
    @Autowired
    private ISysDepartService departService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取导入模板文件
     * @return
     */
    @Override
    protected  String getTemplateFilePath(){
        String path = "excelTemplate/import/leagueMem.xlsx";
        return path;
    }

    /**
     * 保存导入的数据
     */
    @Override
    public void saveData(LeagueMem data){
        String stdNo = data.getStdNo();
        if(StringUtils.isEmpty(stdNo)){
            throw new CustomRuntimeException("学号不能为空！");
        }
        SysUser sysUser = sysUserService.getUserByName(stdNo.trim());
        if(sysUser==null){
            throw new CustomRuntimeException("学号【"+stdNo+"】不存在！");
        }

        LeagueMem leagueMem = leagueMemService.getByUserId(sysUser.getId());
        if (leagueMem != null) {
            log.info("团员已存在，学号：{}", stdNo);
            BeanUpdateUtils.copyNullProperties(data,leagueMem);

            leagueMem.setUserId(sysUser.getId());
            leagueMem.setDelFlag(CommonConstant.DEL_FLAG_0);
            leagueMemService.updateById(leagueMem);
        }else{
            data.setUserId(sysUser.getId());
            data.setDelFlag(CommonConstant.DEL_FLAG_0);
            leagueMemService.save(data);
        }

    }


    /**
     * 导入模板名称
     * @return
     */
    @Override
    public String getDemoFileName(){
            return "团员导入模板";
    }

}
